package com.hd.manager.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hd.hdxgddcommon.entity.dto.ResultDto;
import com.hd.manager.webSocket.DeviceEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;

import static com.hd.manager.webSocket.EndpointManager.getEnpoint;

/**
 * @projectName:    entrance_guard 
 * @package:        com.hd.manager.util
 * @className:      JieShiActivateRequestUtil
 * @author:     otto
 * @description:  
 * @date:    2023/7/24 15:07
 * @version:    1.0
 */ 

@Slf4j
@Component
public class JieShiActivateRequestUtil {
    /**
     * @Param type 请求类型，发送请求时会打印日志
     * @param requestMap 用于获取session
     */
    // 线程安全的HashMap long型 的pkgId  String型的message
    private static final ConcurrentHashMap<Long,String> productMap = new ConcurrentHashMap<>();

    public String getMessage(Long requestId){
        if(!productMap.isEmpty()&&productMap.containsKey(requestId)&&!productMap.get(requestId).equals("")){
            String message = productMap.get(requestId);
            productMap.remove(requestId);
            return message;
        }
        else
            return "";
    }

    public void setMessage(Long requestId,JSONObject jsonObject){
        if(requestId!=null&&productMap.containsKey(requestId)){
            log.info("消息map插入信息");
            productMap.put(requestId,jsonObject.toString());
        }
    }

    /**
     * 将消费需求挂上map
     * @param requestId
     */
    public void setVoidKey(Long requestId){
        productMap.put(requestId,"");
    }

    @Autowired
    private DeviceEndpoint deviceEndpoint;
    public JSONObject activateRequest(HashMap<String,Object> requestMap) throws InterruptedException {
        // 获取session
        /*先获取用户列表*/
        String requestType = (String) requestMap.get("request_type");

        log.info("发起"+requestType+"请求");
        String deviceId = (String) requestMap.get("device_id");
        DeviceEndpoint deviceEndpoint = getEnpoint(deviceId);

        String message = JSON.toJSONString(requestMap);
        System.out.println("message \n "+message);
        try{
            deviceEndpoint.sendMessage(message);
            Long requestId =Long.valueOf(requestMap.get("request_id").toString());
            this.setVoidKey(requestId);
            String result = "";
            while(result.equals("")){
                Thread.sleep(200);
                result = this.getMessage(requestId);
            }
            JSONObject jsonObject = JSON.parseObject(result);
            return jsonObject;
        }catch (Exception e){
            log.error(requestType+"请求失败！");
            log.error(e.toString());
            return null;
        }
    }
}
